package com.ruoyi.data.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.data.domain.bo.Devices.DevicesOldBo;
import com.ruoyi.data.domain.vo.DevicesOldVo;
import com.ruoyi.data.domain.DevicesOld;
import com.ruoyi.data.mapper.DevicesOldMapper;
import com.ruoyi.data.service.IDevicesOldService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备历史Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-12
 */
@RequiredArgsConstructor
@Service
public class DevicesOldServiceImpl implements IDevicesOldService {

    private final DevicesOldMapper baseMapper;

    /**
     * 查询设备历史
     */
    @Override
    public DevicesOldVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询设备历史列表
     */
    @Override
    public TableDataInfo<DevicesOldVo> queryPageList(DevicesOldBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DevicesOld> lqw = buildQueryWrapper(bo);
        Page<DevicesOldVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设备历史列表
     */
    @Override
    public List<DevicesOldVo> queryList(DevicesOldBo bo) {
        LambdaQueryWrapper<DevicesOld> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DevicesOld> buildQueryWrapper(DevicesOldBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DevicesOld> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), DevicesOld::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), DevicesOld::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getProject()), DevicesOld::getProject, bo.getProject());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), DevicesOld::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceGroup()), DevicesOld::getDeviceGroup, bo.getDeviceGroup());
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), DevicesOld::getDeviceName, bo.getDeviceName());
        lqw.eq(StringUtils.isNotBlank(bo.getColumns()), DevicesOld::getColumns, bo.getColumns());
        lqw.eq(StringUtils.isNotBlank(bo.getConfig()), DevicesOld::getConfig, bo.getConfig());
        lqw.eq(StringUtils.isNotBlank(bo.getActionMessage()), DevicesOld::getActionMessage, bo.getActionMessage());
        lqw.eq(StringUtils.isNotBlank(bo.getBattery()), DevicesOld::getBattery, bo.getBattery());
        lqw.eq(StringUtils.isNotBlank(bo.getRuntime()), DevicesOld::getRuntime, bo.getRuntime());
        lqw.eq(bo.getDate() != null, DevicesOld::getDate, bo.getDate());
        lqw.eq(bo.getLastUpdate() != null, DevicesOld::getLastUpdate, bo.getLastUpdate());
        lqw.eq(bo.getResourceId() != null, DevicesOld::getResourceId, bo.getResourceId());
        lqw.eq(bo.getIsScriptRunning() != null, DevicesOld::getIsScriptRunning, bo.getIsScriptRunning());
        lqw.eq(bo.getStatus() != null, DevicesOld::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增设备历史
     */
    @Override
    public Boolean insertByBo(DevicesOldBo bo) {
        DevicesOld add = BeanUtil.toBean(bo, DevicesOld.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备历史
     */
    @Override
    public Boolean updateByBo(DevicesOldBo bo) {
        DevicesOld update = BeanUtil.toBean(bo, DevicesOld.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DevicesOld entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除设备历史
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
